package com.xcc.shopanim;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xcc.shopanim.newa.MainActivity;

public class OtherFragment extends BaseFragment {
    private static final int[] colors = new int[]{
            0xFFFF0000,
            0xffFF00B5,
            0xffEE00FF,
            0xffAD00FF,
            0xff5200FF,
            0xff0026FF,
            0xff00B7FF,
            0xff00FFDA,
            0xff00FF5C,
            0xff0AFF00,
            0xffB9FF00,
            0xffFFFD00,
            0xffFFAF00,
            0xffFF4E00};
    private static int index;
    private boolean open;

    public static synchronized int getColor() {
        int color = colors[index];
        if (++index >= colors.length) index = 0;
        return color;
    }

    public static OtherFragment newInstance() {
        return newInstance(false);
    }

    public static OtherFragment newInstance(boolean open) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("open", open);
        OtherFragment fragment = new OtherFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) return;
        open = bundle.getBoolean("open");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_other, container, false);
        view.setBackgroundColor(getColor());
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (open && view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-2, -2);
            layoutParams.gravity = Gravity.CENTER;
            Button button = new Button(getActivity());
            button.setText("新的动画");
            button.setOnClickListener(v -> MainActivity.open(getActivity()));
            button.setLayoutParams(layoutParams);
            viewGroup.addView(button);
        }
    }
}
